package com.probiz.estoresa.order.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.common.model.order.OrderPayment;
import com.probiz.estore.order.service.OrderPaymentManager;

public class OrderRefundController extends GenericStoreAdminController<OrderPayment> {
    private OrderPaymentManager orderPaymentManager = null;

    public void setOrderPaymentManager(OrderPaymentManager inMgr) {
        this.orderPaymentManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(OrderPayment entity) {
		return entity.getOrderPaymentName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = orderPaymentManager;
	}
	
	@Override
	protected Map<String,String> getSortMap4Direction(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "documentNo asc");
		sortMap.put("2", "documentNo desc");
		sortMap.put("3", "salesOrder.orderNo asc");
		sortMap.put("4", "salesOrder.orderNo desc");
		sortMap.put("5", "paymentAmount asc");
		sortMap.put("6", "paymentAmount desc");
		sortMap.put("7", "convert_gbk(paymentTypeDesc) asc");
		sortMap.put("8", "convert_gbk(paymentTypeDesc) desc");
		sortMap.put("9", "createTime asc");
		sortMap.put("10", "createTime desc");
		return sortMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, OrderPayment entity, BindException errors) {
	}

	public ModelAndView search(HttpServletRequest request, HttpServletResponse response) {
		SearchCriteria sc = createSearchCriteria(request, OrderPayment.FilterHqlName.SEARCH_4_REFUND);
		List results = searchByCriteria(sc);
		return getModelAndView(listView, listModelName, results);
	}

	/**
	 * 打印退款单
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView printRefundList(HttpServletRequest request, HttpServletResponse response) {
		List<OrderPayment> orderPaymentList = new ArrayList<OrderPayment>();
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String id: ids) {
				OrderPayment op=orderPaymentManager.getById(Integer.valueOf(id));
				orderPaymentList.add(op);
			}
			
		}
		ModelAndView mv = new ModelAndView("order/print/orderRefundPrintList");
		mv.addObject("orderPaymentList", orderPaymentList);
		return mv;
	}

}
